package com.chuke.springbootinit.manager;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuke.springbootinit.common.BaseResponse;
import com.chuke.springbootinit.common.ErrorCode;
import com.chuke.springbootinit.common.ResultUtils;
import com.chuke.springbootinit.datasource.*;
import com.chuke.springbootinit.exception.ThrowUtils;
import com.chuke.springbootinit.model.dto.post.PostQueryRequest;
import com.chuke.springbootinit.model.dto.search.SearchRequest;
import com.chuke.springbootinit.model.dto.user.UserQueryRequest;
import com.chuke.springbootinit.model.entity.Picture;
import com.chuke.springbootinit.model.enums.SearchTypeEnum;
import com.chuke.springbootinit.model.vo.PostVO;
import com.chuke.springbootinit.model.vo.SearchVO;
import com.chuke.springbootinit.model.vo.UserVO;
import com.chuke.springbootinit.service.PictureService;
import com.chuke.springbootinit.service.PostService;
import com.chuke.springbootinit.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class SearchFacade {

    @Resource
    private UserDataSource userDataSource;

    @Resource
    private PostDataSource postDataSource;

    @Resource
    private PictureDataSource pictureDataSource;

    @Resource
    private DataSourceRegistry dataSourceRegistry;

    public SearchVO searchAll(@RequestBody SearchRequest searchRequest, HttpServletRequest request) throws IOException {
        String type = searchRequest.getType();
        SearchTypeEnum searchTypeEnum = SearchTypeEnum.getEnumByValue(type);
        // 存疑，如果为空应该查询所有数据而不是抛出异常
//        ThrowUtils.throwIf(StringUtils.isBlank(type), ErrorCode.PARAMS_ERROR);
        String searchText = searchRequest.getSearchText();
        long current = searchRequest.getCurrent();
        long pageSize = searchRequest.getPageSize();
        if (searchTypeEnum == null) {

            Page<UserVO> userVOPage = userDataSource.doSearch(searchText, current, pageSize);

            Page<PostVO> postVOPage = postDataSource.doSearch(searchText, current, pageSize);

            Page<Picture> picture = pictureDataSource.doSearch(searchText, current, pageSize);

            SearchVO searchVO = new SearchVO();
            searchVO.setUserList(userVOPage.getRecords());
            searchVO.setPostList(postVOPage.getRecords());
            searchVO.setPictureList(picture.getRecords());
            return searchVO;
        } else {
            SearchVO searchVO = new SearchVO();

            DataSource<?> dataSource = dataSourceRegistry.getDataSourceByType(type);
            Page<?> page = dataSource.doSearch(searchText, current, pageSize);
            searchVO.setDataList(page.getRecords());
            searchVO.setTotal(page.getTotal());
            return searchVO;
        }
    }

}
